package com.ruoyi.project.manage.park.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.manage.park.domain.DwThemePark;
import com.ruoyi.project.manage.park.service.IDwThemeParkService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 主题乐园信息Controller
 * 
 * @author mhg
 * @date 2021-10-04
 */
@Controller
@RequestMapping("/manage/park")
public class DwThemeParkController extends BaseController
{
    private String prefix = "manage/park";

    @Value("${my_upload_path}")
    private String dirPath;

    @Autowired
    private IDwThemeParkService dwThemeParkService;

    @RequiresPermissions("manage:park:view")
    @GetMapping()
    public String park()
    {
        return prefix + "/park";
    }

    /**
     * 查询主题乐园信息列表
     */
    @RequiresPermissions("manage:park:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DwThemePark dwThemePark)
    {
        startPage();
        List<DwThemePark> list = dwThemeParkService.selectDwThemeParkList(dwThemePark);
        return getDataTable(list);
    }

    /**
     * 导出主题乐园信息列表
     */
    @RequiresPermissions("manage:park:export")
    @Log(title = "主题乐园信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DwThemePark dwThemePark)
    {
        List<DwThemePark> list = dwThemeParkService.selectDwThemeParkList(dwThemePark);
        ExcelUtil<DwThemePark> util = new ExcelUtil<DwThemePark>(DwThemePark.class);
        return util.exportExcel(list, "主题乐园信息数据");
    }

    /**
     * 新增主题乐园信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存主题乐园信息
     */
    @RequiresPermissions("manage:park:add")
    @Log(title = "主题乐园信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(String name, String parkDesc, String limitType, String limitDesc, String faciType, MultipartFile file) throws IOException {
        logger.info("文件上传路径为--："+ dirPath);
        DwThemePark dwThemePark = new DwThemePark();
        dwThemePark.setName(name);
        dwThemePark.setParkDesc(parkDesc);
        dwThemePark.setLimitType(limitType);
        dwThemePark.setLimitDesc(limitDesc);
        dwThemePark.setFaciType(faciType);

        //保存文件到相关路径
        //得到原始文件名
        String fileName = file.getOriginalFilename();
        //得到后缀名
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //得到唯一文件名
        fileName = UUID.randomUUID()+suffix;
        //保存图片的路径
        File dirFile = new File(dirPath);
        //判断如果文件夹不存在 创建
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //得到文件的完整路径
        String filePath = dirPath+"/"+fileName;
        //保存文件  异常抛出
        file.transferTo(new File(filePath));
        //保存文件名
        dwThemePark.setImagePath(fileName);
        return toAjax(dwThemeParkService.insertDwThemePark(dwThemePark));
    }

    /**
     * 修改主题乐园信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DwThemePark dwThemePark = dwThemeParkService.selectDwThemeParkById(id);
        mmap.put("dwThemePark", dwThemePark);
        return prefix + "/edit";
    }

    /**
     * 修改保存主题乐园信息
     */
    @RequiresPermissions("manage:park:edit")
    @Log(title = "主题乐园信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DwThemePark dwThemePark)
    {
        return toAjax(dwThemeParkService.updateDwThemePark(dwThemePark));
    }

    /**
     * 删除主题乐园信息
     */
    @RequiresPermissions("manage:park:remove")
    @Log(title = "主题乐园信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dwThemeParkService.deleteDwThemeParkByIds(ids));
    }
}
